<?php
/**
 * Created by PhpStorm.
 * User: huangpeng3
 * Date: 2018/1/16
 * Time: 上午11:30
 */
namespace Home\Model;

use Think\Model;

class DirModel extends Model
{

    protected $tableName = 'notebook_dirs';


    /**
     * @param $uid
     * @param $dirname
     * @return bool
     * create notebook dir
     */
    public function createNoteDir($uid, $dirname)
    {

        $data['dirname'] = $dirname;
        $data['uid'] = $uid;
        if (empty($this->where($data)->select())) {
            if ($this->add($data)) {
                return 200;
            } else {
                return -1;
            }
        } else {
            return -2;
        }
    }

    /**
     * @param $did
     * @param $dirname
     * @return int
     */
    public function renameNoteDir($did, $dirname)
    {
        $data['dirname'] = $dirname;
        $data['id'] = $did;
        if (empty($this->where($data)->select())) {
            if ($this->where('id=' . $did)->save($data)) {
                return 200;
            } else {
                return -1;
            }
        } else {
            return -2;
        }
    }

    /**
     * @param $did
     * @return int
     */
    public function delNoteDir($did)
    {
        $data['id'] = $did;
        if ($this->where('id=' . $did)->delete()) {
            return 200;
        } else {
            return -1;
        }
    }

    /**
     * @param $uid
     * @param $did
     * @return int
     */
    public function createNoteFile($uid, $did)
    {
        $model = D('notebook_files');
        $cnt = $model->where('uid=' . $uid . ' and did=' . $did)->count();
        $newFilename = 'notebook' . ++$cnt;
        $data['uid'] = $uid;
        $data['did'] = $did;
        $data['fname'] = $newFilename;
        $cid = $model->add($data);
        if ($cid) {
            $rtn['did'] = $did;
            $rtn['cid'] = $cid;
            $rtn['cname'] = $newFilename;
            return $rtn;
        } else {
            return -1;
        }
    }

    /**
     * @param $did
     * @param $cid
     * @param $cname
     * @return int
     */
    public function renameNoteFile($uid, $did, $cid, $cname)
    {
        $data['uid'] = $uid;
        $data['cid'] = $cid;
        $data['did'] = $did;
        $data['fname'] = $cname;
        $model = D('notebook_files');
        if (empty($model->where($data)->select())) {
            if ($model->where('id=' . $cid)->save($data)) {
                return 200;
            } else {
                return -1;
            }
        } else {
            return -2;
        }
    }

    /**
     * @param $uid
     * @param $did
     * @param $cid
     * @param $code
     * @return int
     */
    public function saveNoteFile($uid, $did, $cid, $code)
    {
        $data['uid'] = $uid;
        $data['id'] = $cid;
        $data['did'] = $did;
        $data['code'] = $code;
        $model = D('notebook_files');
        if ($model->where('uid=' . $uid . ' and id=' . $cid . ' and did=' . $did)->save($data)) {
            return 200;
        } else {
            return -1;
        }
    }

    /**
     * @param $uid
     * @param $did
     * @param $cid
     * @return int
     */
    public function delFile($uid, $did, $cid)
    {
        $model = $model = D('notebook_files');
        if ($model->where('uid=' . $uid . ' and did=' . $did . ' and id=' . $cid)->delete()) {
            return 200;
        } else {
            return -1;
        }
    }

    /**
     * @param $uid
     * @return array|int
     */
    public function findall($uid)
    {
        $cur = M();
        $rst = $cur->query("select d.id did, d.dirname,  f.id fid , f.fname from notebook_files f INNER JOIN notebook_dirs d on f.did = d.id order by d.id, f.id");
        if (isset($rst)) {
            $rtn = array();
            foreach ($rst as $k => $v) {
                $did = $v['did'];
                $dirname = $v['dirname'];
                $fid = $v['fid'];
                $fname = $v['fname'];
                $arr_key = $did . '_' . $dirname;
                $arr_val = $fid . '_' . $fname;
                if (!isset($rtn[$arr_key])) {
                    $rtn[$arr_key] = array();
                }
                array_push($rtn[$arr_key], $arr_val);
            }
            return $rtn;
        } else {
            return -1;
        }
    }
}
